<?php

/**
 * Description of Button
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace OverFish\Controls\Grid;

/**
 * Action button
 *
 * @author Jan Marek
 * @license MIT
 */
class Button extends BaseButton {

    /** @var bool */
    private $ajax = false;
    /** @var string|callback|null */
    private $confirmationQuestion = null;

    /**
     * Is ajax?
     * @return bool
     */
    public function isAjax() {
        return $this->ajax;
    }

    /**
     * Set ajax mode
     * @param bool ajax
     * @return Button
     */
    public function setAjax($ajax) {
        $this->ajax = (bool) $ajax;
        return $this;
    }

    /**
     * Get confirmation question
     * @param mixed row
     * @return string|callback|null
     */
    public function getConfirmationQuestion($row) {
        if (is_callable($this->confirmationQuestion)) {
            return call_user_func($this->confirmationQuestion, $row);
        } else {
            return $this->confirmationQuestion;
        }
    }

    /**
     * Set confirmation question
     * @param string|callback|null confirmation question
     * @return Button
     */
    public function setConfirmationQuestion($confirmationQuestion) {
        $this->confirmationQuestion = $confirmationQuestion;
        return $this;
    }

    /**
     * Handle click signal
     * @param string security token
     * @param mixed primary key
     */
    public function handleClick($token, $uniqueId = null) {
        parent::handleClick($token, $uniqueId);

        if ($this->getPresenter()->isAjax()) {
            $this->getGrid()->invalidateControl();
        } else {
            $this->getGrid()->redirect("this");
        }
    }


}

